import json
import redis


def load_sample_data(host='localhost', port=6379, db=0, key='anomaly_data'):
    """
    将示例异常数据加载到Redis中。
    
    参数:
        host (str): Redis主机地址
        port (int): Redis端口
        db (int): Redis数据库编号
        key (str): 存储数据的Redis键
    """
    # 示例异常数据
    sample_data = {
        "anomalies": [
            {
                "id": "cpu_usage_system_server-01_1703123456",
                "type": "cpu",
                "measurement": "cpu",
                "field": "usage_system",
                "severity": "alert",
                "metrics": {
                    "usage_system": 85.5,
                    "usage_user": 65.2,
                    "usage_idle": 14.5
                },
                "start_time": "2025-07-11T11:48:07.123456+00:00",
                "duration": 45,
                "error_frequency": 8,
                "business_impact": "moderate",
                "affected_services": ["web", "api", "database"],
                "device": "server-01",
                "tags": {
                    "cpu": "cpu-total",
                    "host": "server-01",
                    "datacenter": "dc-east"
                },
                "threshold": 80.0,
                "current_value": 85.5,
                "complexity_score": 12,
                "logs": [
                    "!Time: 2025-07-11 11:48:07, Measurement: cpu, Field: usage_system, Value: 85.5, Device: server-01, Error: High CPU usage detected",
                    "!Time: 2025-07-11 11:48:09, Measurement: cpu, Field: usage_system, Value: 86.2, Device: server-01, Error: CPU usage continues to rise"
                ],
                "context": {
                    "system_load": 4.5,
                    "memory_usage": 78.2,
                    "disk_io": "high",
                    "network_traffic": "normal"
                }
            },
            {
                "id": "memory_usage_server-01_1703123500",
                "type": "memory",
                "measurement": "mem",
                "field": "used_percent",
                "severity": "warning",
                "metrics": {
                    "used_percent": 78.2,
                    "available": 2048000,
                    "total": 8388608
                },
                "start_time": "2025-07-11T11:48:07.123456+00:00",
                "duration": 30,
                "error_frequency": 5,
                "business_impact": "low",
                "affected_services": ["web"],
                "device": "server-01",
                "tags": {
                    "host": "server-01",
                    "datacenter": "dc-east"
                },
                "threshold": 75.0,
                "current_value": 78.2,
                "complexity_score": 8,
                "logs": [
                    "!Time: 2025-07-11 11:48:07, Measurement: mem, Field: used_percent, Value: 78.2, Device: server-01, Warning: Memory usage above threshold"
                ],
                "context": {
                    "system_load": 4.5,
                    "cpu_usage": 85.5,
                    "swap_usage": 15.3,
                    "process_count": 245
                }
            },
            # 添加更多示例数据
            {
                "id": "disk_usage_server-02_1703123600",
                "type": "disk",
                "measurement": "disk",
                "field": "used_percent",
                "severity": "critical",
                "metrics": {
                    "used_percent": 95.8,
                    "free": 10240000,
                    "total": 1073741824
                },
                "start_time": "2025-07-11T11:48:07.123456+00:00",
                "duration": 180,
                "error_frequency": 12,
                "business_impact": "high",
                "affected_services": ["database", "storage"],
                "device": "server-02",
                "tags": {
                    "path": "/var/lib/mysql",
                    "host": "server-02",
                    "datacenter": "dc-east"
                },
                "threshold": 90.0,
                "current_value": 95.8,
                "complexity_score": 18,
                "logs": [
                    "!Time: 2025-07-11 11:48:07, Measurement: disk, Field: used_percent, Value: 95.8, Device: server-02, Error: Critical disk space shortage",
                    "!Time: 2025-07-11 11:48:20, Measurement: disk, Field: used_percent, Value: 96.2, Device: server-02, Error: Disk space continues to decrease"
                ],
                "context": {
                    "system_load": 3.2,
                    "memory_usage": 65.5,
                    "io_wait": "very high",
                    "disk_write_rate": "high"
                }
            },
            {
                "id": "network_packets_dropped_server-03_1703123700",
                "type": "network",
                "measurement": "net",
                "field": "err_out",
                "severity": "warning",
                "metrics": {
                    "err_out": 345,
                    "err_in": 120,
                    "packets_in": 45000,
                    "packets_out": 38000
                },
                "start_time": "2023-12-21T10:35:00.123456+00:00",
                "duration": 60,
                "error_frequency": 6,
                "business_impact": "moderate",
                "affected_services": ["api", "web"],
                "device": "server-03",
                "tags": {
                    "interface": "eth0",
                    "host": "server-03",
                    "datacenter": "dc-west"
                },
                "threshold": 200,
                "current_value": 345,
                "complexity_score": 10,
                "logs": [
                    "!Time: 2023-12-21 10:35:00, Measurement: net, Field: err_out, Value: 345, Device: server-03, Warning: Network packet errors detected"
                ],
                "context": {
                    "bandwidth_usage": "high",
                    "system_load": 2.8,
                    "connection_count": 2450,
                    "latency": "increased"
                }
            }
        ],
        "analysis_request": {
            "request_id": "req_1703123456",
            "timestamp": "2023-12-21T10:32:00.000000+00:00",
            "priority": "high",
            "analysis_type": "comprehensive",
            "include_recommendations": True,
            "include_root_cause": True
        }
    }
    
    try:
        # 连接到Redis
        redis_client = redis.Redis(host=host, port=port, db=db)
        
        # 将数据转换为JSON字符串
        json_data = json.dumps(sample_data)
        
        # 存储到Redis
        redis_client.set(key, json_data)
        
        print(f"示例数据已成功加载到Redis，键名: {key}")
        return True
    except Exception as e:
        print(f"将数据加载到Redis时出错: {str(e)}")
        return False


if __name__ == "__main__":
    # 将示例数据加载到Redis
    load_sample_data() 